package com.woniu.controller;

import com.woniu.service.PatientSurgeryService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/patientSurgery")
public class PatientSurgeryController {
    @Resource
    private PatientSurgeryService patientSurgeryService;

    @GetMapping("/list")
    public ResponseUtil selectAll(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return patientSurgeryService.findAll(pageNum, pageSize);
    }

    @GetMapping("/search")
    public ResponseUtil selectByCondition(@RequestParam(required = false) String patientName,
                                          @RequestParam(required = false) String doctorName,
                                          @RequestParam(required = false) Integer status,
                                          @RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        return patientSurgeryService.selectByCondition(patientName, doctorName, status, pageNum, pageSize);
    }

    @PostMapping("/complete")
    public ResponseUtil completeSurgery(@RequestBody Map<String, Object> request) {
        Integer id = (Integer) request.get("id");
        return patientSurgeryService.completeSurgery(id);
    }

    @PostMapping("/sync")
    public ResponseUtil syncData() {
        try {
            patientSurgeryService.syncFromSurgeryApplication();
            return ResponseUtil.get(ResponseEnum.OK, "数据同步成功");
        } catch (Exception e) {
            return ResponseUtil.get(ResponseEnum.FAIL, "数据同步失败");
        }
    }
}
